package com.tjmc.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.finance.mapper.MemberInfoMapper;
import com.tjmc.finance.service.MemberCompanyEquityConfService;
import com.tjmc.finance.service.MemberInfoService;
import com.tjmc.finance.service.MemberPersonalEquityConfService;
import com.tjmc.recruit.admin.api.enums.PlatUserTypeEnum;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.finance.api.entity.MemberCompanyEquityConfEntity;
import com.tjmc.recruit.finance.api.entity.MemberInfoEntity;
import com.tjmc.recruit.finance.api.entity.MemberPersonalEquityConfEntity;
import com.tjmc.recruit.finance.api.vo.MemberInfoVO;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

/**
 * 会员表
 *
 * @author recruit
 * @date 2024-11-25 14:16:59
 */
@Service
@RequiredArgsConstructor
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfoEntity> implements MemberInfoService {
    private final MemberCompanyEquityConfService memberCompanyEquityConfService;
    private final MemberPersonalEquityConfService memberPersonalEquityConfService;

    /**
     * 根据ID查询
     * @param memberId 会员ID
     * @return
     */
    @Override
    public R<MemberInfoVO> getMemberById(Long memberId) {
        MemberInfoEntity memberInfoEntity = baseMapper.selectById(memberId);
        return getMemberInfo(memberInfoEntity);
    }

    /**
     * 根据用户ID查询
     * @param accountId 用户ID
     * @return
     */
    @Override
    public R<MemberInfoVO> getMemberByUserId(Long accountId) {
        MemberInfoEntity memberInfoEntity = baseMapper.selectOne(new LambdaQueryWrapper<MemberInfoEntity>().eq(MemberInfoEntity::getAccountId, accountId));
        return getMemberInfo(memberInfoEntity);
    }

    @NotNull
    private R<MemberInfoVO> getMemberInfo(MemberInfoEntity memberInfoEntity) {
        MemberInfoVO memberInfoVO = new MemberInfoVO();
        if (memberInfoEntity == null) {
            return R.failed("会员信息不存在");
        }
        if (memberInfoEntity.getMemberType().equals(PlatUserTypeEnum.COMPANY)) {
            MemberCompanyEquityConfEntity equityConf = memberCompanyEquityConfService.getById(memberInfoEntity.getEquityId());
            memberInfoVO.setCompanyEquityConf(equityConf);
        } else {
            MemberPersonalEquityConfEntity equityConf = memberPersonalEquityConfService.getById(memberInfoEntity.getEquityId());
            memberInfoVO.setPersonalEquityConf(equityConf);
        }
        memberInfoVO.setStatus(memberInfoEntity.getStatus());
        return R.ok(memberInfoVO);
    }
}
